//
//  TKL_CityPhotoVC.m
//  TKL_EnPro
//
//  Created by Mrzhang on 2019/4/27.
//  Copyright © 2019年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_CityPhotoVC.h"
#import "Head.h"
#import "TKL_cityWebVC.h"
#import "TKL_cityListCell.h"
#import "TKL_CityDetailVC.h"
@interface TKL_CityPhotoVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UISearchBarDelegate>

@property(nonatomic,assign)NSInteger currentPage;
@property(nonatomic,strong)UIView* topView;
@property(nonatomic,strong)NSMutableArray* typeArr;
@property(nonatomic,strong)NSMutableArray* dataSource;
@property(nonatomic,strong)UICollectionView* listCollection;
@property(nonatomic,strong)UIButton* cityBtn;
@property(nonatomic,strong)NSString* cityStr;
@end

@implementation TKL_CityPhotoVC
#pragma mark - getter and setter
-(UIButton *)cityBtn
{
    if (!_cityBtn) {
        @WeakSelf;
        _cityBtn = [[UIButton alloc]init];
        _cityBtn.width=200;
        _cityBtn.height=44;
        _cityBtn.x=0;
        _cityBtn.y=0;;
        [_cityBtn setEnlargeEdge:15];
        [_cityBtn setTitle:@"青岛 >" forState:UIControlStateNormal];
        [_cityBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
        _cityBtn.titleLabel.font = SYSTEMFONT(15);
        
        [_cityBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            [BRStringPickerView showStringPickerWithTitle:@"旅拍城市" dataSource:@[@"青岛",@"大理",@"普吉岛",@"巴厘岛",@"日本"] defaultSelValue:nil resultBlock:^(id selectValue) {
                self.cityStr=selectValue;
                [_cityBtn setTitle:[NSString stringWithFormat:@"%@ >",selectValue] forState:UIControlStateNormal];
                [self loadData];
            }];
        }];
    }
    return _cityBtn;
}
-(NSMutableArray *)typeArr
{
    if (!_typeArr) {
        _typeArr=[NSMutableArray new];
    }
    return _typeArr;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource=[NSMutableArray new];
    }
    return _dataSource;
}
-(UIView *)topView{
    if (!_topView) {
        _topView=[UIView new];
        _topView.x=0;
        _topView.y=0;
        _topView.width=200;
        _topView.height=44;
        _topView.backgroundColor=[UIColor whiteColor];
    }
    return _topView;
}
-(UICollectionView *)listCollection
{
    if (!_listCollection)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat size = (SCREEN_WIDTH-30)/2;
        
        [flowLayout setItemSize:CGSizeMake(size, size+WFCGFloatY(90))];//设置cell的尺寸
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0.0f, 10);//设置其边界
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing =15;
        
        _listCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        
        if (@available(iOS 11.0, *)) {
            _listCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _listCollection.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
        _listCollection.dataSource = self;
        _listCollection.delegate = self;
        [_listCollection setBackgroundColor:HEXCOLOR(@"#f7f7f7")];
        _listCollection.showsVerticalScrollIndicator=NO;
        _listCollection.showsHorizontalScrollIndicator=NO;
        //注册Cell，必须要有
        [_listCollection registerClass:[TKL_cityListCell class] forCellWithReuseIdentifier:@"colcell"];
        
        @WeakSelf;
        _listCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            selfp.currentPage = 1;
            [selfp loadData];
            [selfp.listCollection.mj_header endRefreshing];
            
        }];
        
    }
    return _listCollection;
}
#pragma mark - event
-(void)refresdata
{
    [self loadData];
}
#pragma mark - method
-(void)loadData
{
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"city_photo"];
    [bquery whereKey:@"city" equalTo:self.cityStr];
    //查找GameScore表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            //打印playerName
            NSLog(@"obj.playerName = %@", [obj objectForKey:@"name"]);
            //打印objectId,createdAt,updatedAt
            NSLog(@"obj.objectId = %@", [obj objectForKey:@"ishome"]);
            NSLog(@"obj.createdAt = %@", [obj createdAt]);
            NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
            
        }
        self.dataSource=[[NSMutableArray alloc] initWithArray:array];
        [self.listCollection reloadData];
    }];
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.currentPage=1;
    [self viewModelBand];
    self.title=@"资讯视频";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.listCollection];
    [self.topView addSubview:self.cityBtn];
    self.navigationItem.titleView=self.topView;
    self.cityStr=@"青岛";
    [self loadData];
    // Do any additional setup after loading the view.
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TKL_cityListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colcell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    [cell setSourceDic:self.dataSource[indexPath.row]];
    
    return cell;
}
#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    TKL_cityWebVC* vc=[[TKL_cityWebVC alloc] init];
//    vc.information=@{@"url":@"http://bmob-cdn-21893.b0.upaiyun.com/2019/04/28/84991a7c4090ea26805c83648935e627.html"};
//    [self.navigationController pushViewController:vc animated:YES];
    
    TKL_CityDetailVC* mm=[[TKL_CityDetailVC alloc] init];
    mm.type=1;
    mm.passObj=self.dataSource[indexPath.row];
    [self.navigationController pushViewController:mm animated:YES];
    
    
//
}
-(void)requestVideoType
{
    @WeakSelf;
    [[InterfaceManager shareInterface] requestAllInterface:@"http://101.201.171.131:8080/rest/api/home/tag/video" para:[NSMutableDictionary new] handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [selfp.typeArr removeAllObjects];
        NSArray* arr=[info objectForKey:@"dataList"];
        for (NSDictionary* dic in arr) {
            [selfp.typeArr addObject:dic];
        }
        
    }];
}
-(void)requestCityData:(NSMutableDictionary *)para
{
    @WeakSelf;
    //http://discover.hunlimao.com/topic/index?cid=0&page=1
    //base=0&deviceType=1&order=1&page=1&tagId=30000&themeId=12&type=2
    [[InterfaceManager shareInterface] requestAllInterface:@"http://101.201.171.131:8080/rest/api/home/poster/list" para:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        
        if (self.currentPage==1) {
            [selfp.dataSource removeAllObjects];
        }
        NSArray* arr=[info objectForKey:@"dataList"];
        for (NSDictionary* dic in arr) {
            
            [selfp.dataSource addObject:dic];
            
        }
        [selfp.listCollection reloadData];
        
    }];
    // 1544683517694
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
